
#include <stdio.h>
#include <curl/curl.h>

// 回调函数，用于写入下载的数据到文件
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    return fwrite(ptr, size, nmemb, stream);
}

int main(int argc, char *argv[]) {
    CURL *curl_handle;
    FILE *fp;
    const char *url_list[] = {
        "https://example.com/image1.jpg",
        "https://example.com/image2.jpg",
        "https://example.com/image3.jpg"
    };
    const char *output_files[] = {
        "image1.jpg",
        "image2.jpg",
        "image3.jpg"
    };
    int num_urls = sizeof(url_list) / sizeof(url_list[0]);

    char *url = argv[1];

    // 初始化 libcurl
    curl_global_init(CURL_GLOBAL_ALL);

    // 创建 libcurl 句柄
    curl_handle = curl_easy_init();

    // 设置 libcurl 选项
    // curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    for (int i = 0; i < 1; ++i) {
        // 打开文件以写入下载的数据
        fp = fopen(output_files[i], "wb");
        if (!fp) {
            fprintf(stderr, "Failed to open fp %s\n", output_files[i]);
            continue;
        }

        // 设置 libcurl 下载的 URL
        curl_easy_setopt(curl_handle, CURLOPT_URL, url);

        // 将数据写入文件
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, fp);

        // 执行 HTTP 请求
        curl_easy_perform(curl_handle);

        // 关闭文件
        fclose(fp);
    }

    // 清理 libcurl 资源
    curl_easy_cleanup(curl_handle);

    // 全局清理
    curl_global_cleanup();

    return 0;
}

